package com.kdtech.analyse.Bbs.common;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
/**
 * phpwind v8.5(20110323) 版本
 * 0000
 * http://www.xxx.xxx/ XX论坛
 * @author KK
 */
public class BbsAnalysePhpWind implements AnalyseNews {


	
	public boolean isDetailPage(String url) {
		url = UrlArgumentTop.FromatUrl(url);		
		if(url.endsWith("/")){
			url = url.substring(0, url.length()-1);
			url = StringUtils.substringAfterLast(url, "/");
		}
		
		else if (url.contains("read/")) {
			url = StringUtils.substringAfter(url, "/");
		}
		else if (url.contains("p/")) {
			url = StringUtils.substringAfter(url, "/");
		}
		
		else{
			url = StringUtils.substringAfterLast(url, "/");
		}
		String[] regex = {
				"t[0-9]{3,}",
				"t[0-9]+.html",
				"t-[0-9]+-1-1.html",
				"read-htm-tid-[0-9]+-page-1.htm[l]?",
				"read-forum-tid-[0-9]+.htm",
				"read-htm-tid-[0-9]+(-page-1)?.html",
				"read-tid-[0-9]+.htm[l]?",
				"read.php[?]tid=[0-9]*",
				"topic_[0-9]+.html",
				"thread-[0-9]+-1-1.html",
				"view-[0-9]+.html",
				"read.php\\?tid-[0-9]*.html",
				"read.php\\?fid=[0-9]+&tid=[0-9]+",
				"read-htm-tid-[0-9]*-1.html",
				"read-htm-tid-[0-9]*-fid-[0-9]+.html",
				"read-[0-9]+",
				".*/read/[0-9]+",
				".*/read/[0-9]+.html.*",
				".*/p/[0-9]+",
				"read.php\\?page=1&tid=[0-9]+",
				"read.php\\?fpage=1&tid=[0-9]+",
				"read-htm-tid-[0-9].*.htm",
				"content.aspx\\?board=[0-9]*&class=[0-9]*&page=[0-9]*&topic=[0-9]*",
				"read.php\\?tid-[0-9]*.htm",
				"forum.php\\?extra=.*&mod=viewthread&tid=[0-9]+",
				"article-[0-9]+.html",
				};
		return RegexUtils.matchAny(url, regex);
	}

	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news = new NewsMeta();
		if (urlMeta.getHtml() == null) {
			return null;
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;
		String updateUrl = url;
		String author=null;
		Document doc = Jsoup.parse(htmltxt);
		doc.select("a:containsOwn(登录/注册后可看大图)").remove();
		if(doc.select("div.regForm").size()>0&&doc.select("div.regForm").text().contains("您没有登录或者您没有权限访问此页面")){
			news.setUpdateUrl(ParseState.FORBID.toString());
			return news;
		}

		doc.select("div#relNews").remove();
		doc.select("div#dSjb").remove();
		doc.select("div#dKhd").remove();
		doc.select("div#dPics").remove();
		doc.select("div#weiboLinks").remove();
		doc.select("div#copyright").remove();
		doc.select("div#sideCon").remove();

		try {
			title = doc.select("#subject_tpc,#thread_subject,#subject_").get(0).text();
			title = title.replaceAll("\\[[^\\]]+\\]", "");
			if(title.startsWith("主题：")){
				title = StringUtils.substringAfter(title, "主题：");
			}
		} catch (Exception e) {
			title = doc.select("title").text();
			if (StringUtils.isNotEmpty(title)){
				title = StringUtils.substringBefore(title, "|");
				title = StringUtils.substringBefore(title, "-");
			}
		}

		if(StringUtils.isBlank(title)){
			title = doc.select("h1").text();
		}

		Element firstDiv = doc.select("div#readfloor_tpc").first();
		if (firstDiv!=null){
			date = DateUtils.matchDate(firstDiv.select("div.tipTop.s6 span[title]").attr("title"));
			content = HtmlCleaner.getContentHtml(url,firstDiv.select("div#mainCon"));
			Element firstA = firstDiv.select(" td.floot_left div.readName a").first();
			if(firstA!=null)  author = firstA.text();
			if(StringUtils.isBlank(author)){
				author = firstDiv.select("div.tipTop a.fl").text();
			}
			
			if(StringUtils.isBlank(author)){
				Element last = firstDiv.select("div.tipTop a").last();
				if(last!=null){
					author =last .text();
				}
			}
		}else{
			firstDiv = doc.select("div.floor_main").first();
			if (firstDiv!=null){
				date = DateUtils.matchDate(firstDiv.select("div.thread_hd i").text());
				content = HtmlCleaner.getContentHtml(url,firstDiv.select("div#mainCon"));
			}
		}
		if(StringUtils.isBlank(author)){
			Elements select = doc.select("div.name");
			if(select!=null && select.size()>0){
				author = select.get(0).text();
			}
		}
		if(StringUtils.isBlank(author)){
			Elements select = doc.select("td[width=20%] b");
			if(select!=null && select.size()>0){
				author = select.get(0).text();
			}
		}

		if(date==null){
			date = DateUtils.matchDate(doc.select("div.tipTop span[title]").attr("title"));
			if(date==null){
				try {
					date = DateUtils.matchDate(doc.select("div.tiptop").first().text());
				} catch (Exception e) {
				}
			}
			if(date==null){
				date = DateUtils.matchDate(doc.select("div.authi").text());
			}
			if(date==null){
				date = DateUtils.matchDate(doc.select("div.zuihou_readTop").text());
			}
		}
		if(date==null){
			date = DateUtils.matchDate(doc.select("div.tipBottom,div.floor_top_tips").text());
		}
		if(date==null){
			date = DateUtils.matchDate(doc.select("div.s6").text());
		}
		if(date==null){
			date = DateUtils.matchDate(doc.select("td.smalltxt").text());
		}
		if(date==null){
			date = DateUtils.matchDate(doc.select("span.time").text());
		}

		if(StringUtils.isBlank(content)){
			Elements select = doc.select("div.editor_content");
			if(select!=null && select.size()>0){
				content = HtmlCleaner.getContentHtml(url,select.get(0));
			}
		}
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url,doc.select("div#read_tpc"));
		}
		if(StringUtils.isBlank(content)){
			Element first = doc.select(".t_f").first();
			content = HtmlCleaner.getContentHtml(url, first);
		}
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url,doc.select("div[id^=read_]"));
		}
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url,doc.select("span.tpc_content"));
		}
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url,doc.select("div.content"));
		}

		String text = doc.select("div.readNum ul.cc").text();
		if(StringUtils.isNotBlank(text)){
			clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(text, "|"));
			commnetNum = NumberUtils.matchNumber(StringUtils.substringAfter(text, "|"));
			if(commnetNum == null){
				Elements select = doc.select("li.ft12");
				if (select.size()>1){
					commnetNum = NumberUtils.matchNumber(select.get(1).text());
				}
			}
			if(commnetNum == null){
				commnetNum = NumberUtils.matchNumber(StringUtils.substringAfter(text, "阅读"));
			}
			if(commnetNum == null){
				commnetNum = NumberUtils.matchNumber(StringUtils.substringAfter(text, "ئىنكاس"));
			}
		}else{
			Elements select = doc.select("div.thread_view_info p");
			if(select.size() > 0){
				clickNum = NumberUtils.matchNumber(select.get(0).text());
				commnetNum = NumberUtils.matchNumber(select.get(1).text());
			}
		}

		if(clickNum==null && commnetNum==null){
			Elements select2 = doc.select("span.count em");
			if(select2.size()==2){
				clickNum = NumberUtils.matchNumber(select2.get(0).text());
				commnetNum = NumberUtils.matchNumber(select2.get(1).text());
			}

		}

		if(clickNum==null && commnetNum==null){
			clickNum = NumberUtils.matchNumber(doc.select("div.post_num >.hits>em").text());
			commnetNum = NumberUtils.matchNumber(doc.select("div.post_num >.replies>em").text());

		}

		if(StringUtils.isBlank(author)){
			Elements select = doc.select(".username a");
			if(select!=null && select.size()!=0){
				author=select.get(0).text();
			}
			if(StringUtils.isBlank(author)){
				if(doc.select(".user_name a").size() > 0){
					author = doc.select(".user_name a").get(0).text();
				}
				if(StringUtils.isBlank(author)){
					Element first = doc.select("div.readName a").first();
					if (first!=null){
						author = first.text();
					}
				}
				if(StringUtils.isBlank(author)){
					Element first = doc.select("p.namemsg a").first();
					if (first!=null){
						author = first.text();
					}
				}
				if(StringUtils.isBlank(author)){
					Element first = doc.select(".authi").first();
					if (first!=null){
						author = first.text();
					}
				}
				if(StringUtils.isBlank(author)){
					Element first = doc.select("div.readName").first();
					if (first!=null){
						author = first.text();
					}
				}
				if(StringUtils.isBlank(author)){
					Element first = doc.select("div.r_name a.b").first();
					if (first!=null){
						author = first.text();
					}
				}
				if(StringUtils.isBlank(author)){
					if(doc.select("b.fl").size() > 0)
						author = doc.select("b.fl").get(0).text();
				}
			}
		}


		news.setUrl(url);
		news.setType(4);
		news.setTitle(StringUtils.trimSpace(title));
		news.setCommentNum(commnetNum);
		news.setClickNum(clickNum);
		news.setContent(StringUtils.trimSpace(content));
		news.setDate(date);
		news.setAuthor(author);
		news.setUpdateUrl(updateUrl);
		return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	

	

}
